import { Component, Input, ChangeDetectionStrategy, ChangeDetectorRef, OnChanges } from '@angular/core';
import { GCAssesment, CompanyGlobalCompactService } from '../companyGlobalCompact.service';
import { CompanyGCPrincipleModel } from '../models/companyGCprinciple.model';
import { BaseComponent } from '../../../../../shared/components/baseComponent/base.component';
import { CompanyGCSummaryModel } from '../models/companyGCsummary.model';
import { Console } from '@angular/core/src/console';

/**
 * Company global compact pillar component
 *
 * Author: Sorin Spiru
 * Date: 8.03.2018
 */
@Component({
    moduleId: module.id,
    selector: 'pillar',
    templateUrl: 'companyGlobalCompactPillar.component.html',
    styleUrls: [ '../companyGlobalCompact.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class CompanyGlobalCompactPillarComponent extends BaseComponent implements OnChanges {
    public gCAssessment: typeof GCAssesment = GCAssesment;
    public pillarPrinciples;

    @Input() public pillarId: number;

    @Input() public pillarName: string;

    @Input() public pillarData: CompanyGCPrincipleModel[];

    @Input() public principles: [{id: number, description: string}];

    @Input() public hasBothLists = false;
    @Input() public gcSummary: CompanyGCSummaryModel;

    constructor(
        private cdRef: ChangeDetectorRef,
        public companyGlobalCompactService: CompanyGlobalCompactService) {
        super(cdRef);
    }

    ngOnChanges() {
        this.pillarPrinciples = {};
        if (this.pillarData)
        {
            this.pillarData.forEach((pillarData) => {
                const id = pillarData.principle.id;
                this.pillarPrinciples[id] = this.getPillarPrinciple(pillarData.principle.id);  
            });

        }
    }

    public getPillarPrinciple(principleId: number): CompanyGCPrincipleModel {
        return this.pillarData
            ? this.pillarData.find(x => x.principle.id === principleId)
            : undefined;
    }
}
